<?php


namespace app\common\service;

/**
 * 公式
 * 注：这里汇集了所有的公式
 */
class FormulaService extends BaseService
{
    /**
     * +----------------------------------------------------------
     * 获取单位
     * +----------------------------------------------------------
     * @param string $unit
     * +----------------------------------------------------------
     * @return int
     * @author hongwei 2019-07-01
     * +----------------------------------------------------------
     */
    public function getUnit($unit = 'kg')
    {
        $emissionFunction = 0;
        if ($unit == 'mg') {
            $emissionFunction = 1;
        }
        if ($unit == 'g') {
            $emissionFunction = 1000;
        }
        if ($unit == 'kg') {
            $emissionFunction = 1000000;
        }
        if ($unit == 't') {
            $emissionFunction = 1000000000;
        }
        return $emissionFunction;
    }

    /**
     * 减排前求和求平均
     * @param array $data 数据
     * @param string $type 默认小时均值,hour小时,minute分钟,day天
     * @return float
     * @author hongwei 2019-07-23
     */
    public function beforeSumAndAvg($data, $type = 'hour')
    {
        $days   = 7;
        $result = '';
        if ($type == 'day') {
            $result = $data / $days;        //天均值
        }
        if ($type == 'hour') {
            $result = $data / ($days * 24); //小时均值
        }
        if ($type == 'minute') {
            $result = $data / ($days * 24 * 60); //分钟均值
        }
        return round($result, 2);
    }

    /**
     * 排放量的公式
     * 排放量 = 烟气里 * 运行浓度
     * @param float $gasFlue 烟气里
     * @param float $concentration 运行浓度
     * @return float|int
     * @author hongwei 2019-07-16
     */
    public function emissionFormula($gasFlue, $concentration)
    {
        $result = $gasFlue * $concentration;
        return $result;
    }


    /**
     * 减排完成率
     * @param $completeEmissions
     * @param $targetEmissions
     * @return string
     * @author hongwei 2019-07-10
     */
    public function completionRateOfEmissionReduction($completeEmissions, $targetEmissions)
    {
        //减排完成率 =（完成的减排量/目标减排量 ）* 100%
        $rateEmission = ($completeEmissions - $targetEmissions) * 100;
        $rateEmission = round($rateEmission, 2);
        $rate         = $rateEmission . '%';
        return $rate;
    }

    /**
     * 目标减排量
     * 公式：减排前排放量 - 方案排放量
     * @param string $beforeEmission 减排前排放量
     * @param string $tsinghuaEmission 清华方案排放量
     * @return float
     * @author hongwei 2019-07-10
     */
    public function targetEmission($beforeEmission, $tsinghuaEmission)
    {
        return round($beforeEmission - $tsinghuaEmission, 2);
    }

    /**
     * 排放量填数公式（旧的）
     * @param integer $lostHours 丢失小时数
     * @param integer $wholeHours 全部小时数
     * @param array $oldData 旧数据
     * @return float|int
     * @author hongwei 2019-07-04
     */
    public function supplementData($lostHours, $wholeHours, $oldData)
    {
        $data = $oldData * $wholeHours / ($wholeHours - $lostHours);
        return $data;
    }


    /**
     * 运行浓度实现平均下降 = 减排前平均浓度 - 预案平均浓度
     * @param $reduceBeforeConcentration '减排前平均浓度(7天)'
     * @param $planAverageConcentration '预案平均浓度（小时平均）'
     * @return mixed
     * @author yy by 2019/7/15
     */
    public function getConcentrationAverageDown($reduceBeforeConcentration, $planAverageConcentration)
    {
        return $reduceBeforeConcentration - $planAverageConcentration;
    }

    /**
     * 运行效率实现平均提升 = 预案平均运行效率 - 减排前平均运行效率
     * @param $planAverageEfficiency '预案平均运行效率（小时平均）'
     * @param $reduceBeforeEfficiency '减排前平均运行效率(7天)'
     * @return mixed
     * @author yy by 2019/7/15
     */
    public function getEfficiencyAverageUp($planAverageEfficiency, $reduceBeforeEfficiency)
    {
        return $planAverageEfficiency - $reduceBeforeEfficiency;
    }

    /**
     * 企业响应平均率 = 历史响应数 / 历史被管控数
     * @param $historyResponseNumber '历史响应数'
     * @param $historyControlNumber '历史被管控数'
     * @return float|int
     * @author yy by 2019/7/15
     */
    public function getCompanyResponsivity($historyResponseNumber, $historyControlNumber)
    {
        return $historyControlNumber > 0 ? $historyResponseNumber / $historyControlNumber : '-';
    }

    /**
     * 预案减排完成率 = 完成减排总量 / 预案减排总量 * 100
     * @param $achieveReduceTotal '完成减排总量'
     * @param $planReduceTotal '预案减排总量'
     * @return float|int
     * @author yy by 2019/7/15
     */
    public function getPlanReduceAchieveRate($achieveReduceTotal, $planReduceTotal)
    {
        return  $planReduceTotal > 0 ? round(($achieveReduceTotal / $planReduceTotal * 100),2).'%' : '-';
    }

    /**
     * 贡献度消减平均率 =（减排前贡献度平均值-管控中贡献度平均值）/ 减排前贡献度平均值 * 100
     * @param $reduceBeforeContributionAverage '减排前贡献度平均值'
     * @param $controlContributionAverage '管控中贡献度平均值'
     * @return float|int
     * @author yy by 2019/7/15
     */
    public function getContributionDownRate($reduceBeforeContributionAverage, $controlContributionAverage)
    {
        return $reduceBeforeContributionAverage > 0 ? ($reduceBeforeContributionAverage - $controlContributionAverage) / $reduceBeforeContributionAverage * 100 : '-';
    }

    /**
     * 首先单个企业的管控有效率 = 减排完成量 / 预案减排量 * 100，对多家的求平均
     * @param array $companyArr '多个企业的管控有效率数组集合'
     * @return float|int|string
     * @author yy by 2019/7/15
     */
    public function getAllCompanyControlAverageRate($companyArr = [])
    {
        return count($companyArr) > 0 ? array_sum($companyArr) / count($companyArr) : '-';
    }

    /**
     * 单个企业的管控有效率 = 减排完成量 / 预案减排量 * 100
     * @param $reduceTotal '减排完成量'
     * @param $planTotal '预案减排量'
     * @return float|int
     * @author yy by 2019/7/15
     */
    public function getCompanyControlAverageRate($reduceTotal, $planTotal)
    {
        return $reduceTotal / $planTotal * 100;
    }

    /**
     * 空气质量改善值 = 减排前空气质量值 - 管控中空气质量值
     * @param $reduceAirQualityValue '减排前空气质量值'
     * @param $controlAirQualityValue '管控中空气质量值'
     * @return mixed
     * @author yy by 2019/7/15
     */
    public function getAirQualityImproveValue($reduceAirQualityValue, $controlAirQualityValue)
    {
        return $reduceAirQualityValue - $controlAirQualityValue;
    }

    /**
     * 预案管控有效率=管控改善值/预案推演值*100
     * @param $controlImprove '管控改善值'
     * @param $planDeduce '预案推演值'
     * @return float|int
     * @author yy by 2019/7/15
     */
    public function getPlanControlEffectiveRate($controlImprove, $planDeduce)
    {
        return $controlImprove / $planDeduce * 100;
    }

    /**
     * 预案响应率 = 本批企业预案响应数 / 本批预案企业数 * 100
     * @param $responseNumber '本批企业预案响应数'
     * @param $companyNumber '本批预案企业数'
     * @return float|int|string
     * @author yy by 2019/7/15
     */
    public function getPlanResponseRate($responseNumber, $companyNumber)
    {
        return $companyNumber > 0 ? $responseNumber / $companyNumber * 100 : '-';
    }

    /**
     * 预案减排成本 = 减排用料 * 减排单价
     * @param $reduce '减排用料'
     * @param $univalence '减排单价'
     * @return float|int
     * @author yy by 2019/7/16
     */
    public function getPlanReduceCost($reduce, $univalence)
    {
        return $reduce * $univalence;
    }
}